package com.stu.stusystem.service.system;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.stu.stusystem.common.ApiException;
import com.stu.stusystem.common.OffsetPage;
import com.stu.stusystem.mapper.system.RoleMapper;
import com.stu.stusystem.model.system.Role;
import com.stu.stusystem.model.system.StuUser;
import com.stu.stusystem.service.system.dto.RoleDTO;
import com.stu.stusystem.service.system.vo.RoleVO;
import com.stu.stusystem.util.CopyBeanUtil;
import com.stu.stusystem.util.UUIDUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.weekend.WeekendSqls;

import java.util.Date;
import java.util.List;

/**
 * @author: cxt
 * @time: 2021/3/31
 */
@Service
public class RoleService {

    private RoleMapper roleMapper;

    /**
     * 分页查询角色
     */
    public PageInfo<RoleVO> getRolePage(OffsetPage query, String name) {
        PageHelper.startPage(query.getPage(), query.getLimit());
        Example example = new Example(Role.class);
        Example.Criteria criteria = example.createCriteria();
        if (name.isEmpty()) {
            criteria.andEqualTo("name", name);
        }
        example.orderBy("crete_time").desc();
        List<Role> roles = this.roleMapper.selectByExample(example);
        List<RoleVO> vos = CopyBeanUtil.copyList(roles, RoleVO.class);
        return new PageInfo<>(vos);
    }

    /**
     * 更新角色
     */
    public void updateRole(RoleDTO dto) {
        Role role = this.roleMapper.selectByPrimaryKey(dto.getId());
        if (role == null) {
            throw new ApiException("更新角色不存在");
        }
        CopyBeanUtil.copyIgnore(dto, role);
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        role.setUpdateBy(user.getName());
        role.setUpdateTime(new Date());
        int i = this.roleMapper.updateByExampleSelective(role, new Example.Builder(Role.class)
                .where(WeekendSqls.<Role>custom().andEqualTo(Role::getId, dto.getId())).build());
        if (i != 1) {
            throw new ApiException(ApiException.UPDATE_FAIL);
        }
    }

    /**
     * 删除角色
     */
    public void deleteRole(String id) {
        int i = this.roleMapper.deleteByPrimaryKey(id);
        if (i != 1) {
            throw new ApiException(ApiException.DELETE_FAIL);
        }
    }

    /**
     * 新增角色
     */
    public void saveRole(RoleDTO dto) {
        List<Role> roles = this.roleMapper.selectByExample(new Example.Builder(Role.class)
                .where(WeekendSqls.<Role>custom().andEqualTo(Role::getName, dto.getName())).build());
        if (!roles.isEmpty()) {
            throw new ApiException("新增角色已存在");
        }
        Role role = new Role();
        CopyBeanUtil.copyIgnore(dto, role);
        role.setId(UUIDUtil.generate());
        StuUser user = (StuUser) SecurityUtils.getSubject().getPrincipal();
        role.setCreateBy(user.getName());
        role.setCreateTime(new Date());
        int insert = this.roleMapper.insert(role);
        if (insert != 1) {
            throw new ApiException(ApiException.SAVE_FAIL);
        }
        // 新增成功后进行 权限的更新
        // TODO 角色关系表中，重新设置
        // TODO 内存的权限清除

    }

    @Autowired
    public void setRoleMapper(RoleMapper roleMapper) {
        this.roleMapper = roleMapper;
    }
}
